package Client;

import javax.swing.*;
import java.io.*;
import java.net.*;

class SocketClient extends JFrame{
	private int HOST_PORT = 3200;
	private Socket s;
	InputStream is;
	BufferedReader br;
	OutputStream os;
	BufferedWriter bw;
	String sentMessage="";
	String receivedMessage;
	ObjectInputStream ois;
	ClientGUI tempClientGUI;
	private boolean sanSangKetNoi = false;
	
	public SocketClient(ClientGUI _tempClientGUI) {
		tempClientGUI = _tempClientGUI;
		if (tempClientGUI.isSanSangKetNoi() == false)
			return;
		try 
		{
			s = new Socket("localhost", HOST_PORT);
			
			tempClientGUI.getLblTrangThai().setText("Đã kết nối server");
			
			setSanSangKetNoi(true);
			
			is = s.getInputStream();
			
			br = new BufferedReader(new InputStreamReader(is));
     
			os = s.getOutputStream();
			bw = new BufferedWriter(new OutputStreamWriter(os));
		} 
		catch (IOException e) 
		{
			JOptionPane.showMessageDialog(this, "Server chưa sẵn sàng kết nối", "Lỗi kết nối", JOptionPane.WARNING_MESSAGE);
		}
	}
	public void createInputObject()
	{
		try {
			ois = new ObjectInputStream(s.getInputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void createBR()
	{
		try 
		{	
			is = s.getInputStream();
			
			br = new BufferedReader(new InputStreamReader(is));
     
	
		} 
		catch (IOException e) 
		{
			JOptionPane.showMessageDialog(this, e.toString(), "Lỗi kết nối", JOptionPane.WARNING_MESSAGE);
		}
	}
	/**
	 * @return the sanSangKetNoi
	 */
	public boolean isSanSangKetNoi() {
		return sanSangKetNoi;
	}
	/**
	 * @param sanSangKetNoi the sanSangKetNoi to set
	 */
	public void setSanSangKetNoi(boolean sanSangKetNoi) {
		this.sanSangKetNoi = sanSangKetNoi;
	}
	
}
